package org.dromara.common.sms.handler

import cn.hutool.http.HttpStatus
import io.github.oshai.kotlinlogging.KotlinLogging
import jakarta.servlet.http.HttpServletRequest
import org.dromara.common.core.domain.R
import org.dromara.common.core.domain.R.Companion.fail
import org.dromara.sms4j.comm.exception.SmsBlendException
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice

/**
 * SMS异常处理器
 *
 * @author LikeYouDo
 * @date 2025/7/17
 */
@RestControllerAdvice
class SmsExceptionHandler {
    companion object {
        private val log = KotlinLogging.logger { }
    }

    /**
     * sms异常
     */
    @ExceptionHandler(SmsBlendException::class)
    fun handleSmsBlendException(e: SmsBlendException?, request: HttpServletRequest): R<Void?> {
        val requestURI = request.requestURI
        log.error(e) { "请求地址'$requestURI',发生sms短信异常." }
        return fail<Void?>(HttpStatus.HTTP_INTERNAL_ERROR, "短信发送失败，请稍后再试...")
    }
}
